import { Component, OnInit } from '@angular/core';
import {Globals} from '../../../globals';
import {FormBuilder, FormGroup} from '@angular/forms';
import {PageHelper} from '../../../utils/page-helper-model';
import {RoleIdEnum} from '../../../enums/role-id.enum';
import {UserInfoModel} from '../../../models/user-info.model';
import {BaseService} from '../../../shared/service/base.service';
import {ActivatedRoute, Router} from '@angular/router';
import {Consts} from '../../../config/consts';
import {ApiUrl} from '../../../config/api-url';

@Component({
  selector: 'app-gift-goods',
  templateUrl: './gift-goods.component.html',
  styleUrls: ['./gift-goods.component.less']
})
export class GiftGoodsComponent implements OnInit {
  isMobile = Globals.isMobile();
  listOfData: any;
  formModel: FormGroup;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  // 机构
  agencyList: [];
  // 运营商
  userList: [];
  roleIdEnum = {
    SUPER_ADMINISTRATOR: RoleIdEnum.SUPER_ADMINISTRATOR,
    AGENT: RoleIdEnum.AGENT,
    MERCHANT: RoleIdEnum.MERCHANT,
  };
  // 用户信息
  userInfo: UserInfoModel;
  serverFileUrl: any;
  isVisible: boolean = false;
  fileName: any;

  constructor(private fb: FormBuilder, private baseService: BaseService, private activatedRoute: ActivatedRoute, private router: Router) {
    this.buildForm();
  }


  ngOnInit(): void {
    this.findBackEndUrl();
    this.getAllAgencyOfName();
    this.getAllUserOfName();
    this.userInfo = JSON.parse(sessionStorage.getItem(Consts.USER_INFO));
    this.submitForm();
  }


  buildForm() {
    this.formModel = this.fb.group({
      agencyId: ['', []],
      userId: ['', []],
      keyword: ['', []],
    });
  }

  submitForm() {
    this.pageHelper.currentPage = 1;
    this.findGoodsList();
  }

  onPageSizeChange(pageSize: any){
    // console.log(pageSize, 'pageSize');
    this.pageHelper.pageSize = pageSize;
    this.pageHelper.currentPage = 1;
    this.findGoodsList();
  }
  findGoodsList() {
    this.baseService.findListByMuchCondition(ApiUrl.CommodityUrl + '/list', this.formModel.value, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfData = data.data.list;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  getAllAgencyOfName() {
    this.baseService.get(ApiUrl.AgencyUrl + '/getAllAgencyOfName').subscribe(data => {
      if (data.status == 0) {
        this.agencyList = data.data;
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }

  getAllUserOfName() {
    let agencyId = this.formModel.get('agencyId').value;
    if (agencyId == null) {
      agencyId = '0';
    }
    this.baseService.get(ApiUrl.UserUrl + '/getAllUserOfName?agencyId=' + agencyId).subscribe(data => {
      if (data.status == 0) {
        this.userList = data.data;
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }

  gainAgencyId($event: any) {
    this.getAllUserOfName();
  }

  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findGoodsList();
  }

  skipMerchant(data: any) {
    this.router.navigateByUrl('/merchant?userId=' + data.userId,
      {skipLocationChange: true});
  }

  skipAgency(data) {
    this.router.navigateByUrl('/agency?agencyNo=' + data.agencyNo,
      {skipLocationChange: true});
  }

  findBackEndUrl() {
    this.baseService.get(ApiUrl.Config + '/findBackEndUrl?type=image').subscribe(
      data => {
        if (data.status == 0) {
          if (data.data == null || data.data.url == null) {
            this.baseService.layer.messageError('找不到地址');
            return;
          }
          this.serverFileUrl = data.data.url;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  showModal(data): void {
    this.isVisible = true;
    this.fileName = data.fileName;
  }

  handleCancel(): void {
    this.isVisible = false;
  }
}
